package com.hxit.huaxiamelongarden.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxit.huaxiamelongarden.constants.FriendStatus;
import com.hxit.huaxiamelongarden.domin.po.Friend;
import com.hxit.huaxiamelongarden.mapper.FriendMapper;
import com.hxit.huaxiamelongarden.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FriendServiceImpl
 * @description 好友列表逻辑实现类
 * @Author 12694
 * @Date 2025/1/30 11:21
 * @Version 1.0
 **/
@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendMapper friendMapper;
    //我关注的人--关注
    @Override
    public List<Friend> getMyFriends(int userId) {
        LambdaQueryWrapper<Friend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Friend::getUserId,userId).eq(Friend::getStatus, FriendStatus.FOLLOWED);
        List<Friend> FollowedFriends = friendMapper.selectList(queryWrapper);
        return FollowedFriends;
    }

    //关注我的人--粉丝
    @Override
    public List<Friend> getMyFans(int userId) {
        LambdaQueryWrapper<Friend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Friend::getFriendId,userId).eq(Friend::getStatus, FriendStatus.FOLLOWED);
        List<Friend> FollowedFriends = friendMapper.selectList(queryWrapper);
        return FollowedFriends;
    }

    //互相关注
    @Override
    public List<Friend> getEachOtherFriends(int userId) {
        List<Friend> myFriends = getMyFriends(userId);
        List<Friend> myFans = getMyFans(userId);
        List<Friend> eachOtherFriends = new ArrayList<>();
        if (myFriends != null || myFans != null) {
            for (Friend myFriend : myFriends) {
                for (Friend myFan : myFans) {
                    if(myFriend.getFriendId().equals(myFan.getUserId())){
                        eachOtherFriends.add(myFan);
                    }
                }
            }
            return eachOtherFriends;
        }
        return null;
    }
}
